﻿using TMPro;
using UnityEngine;
using Yoozoo.UI.YGUI;
using Yoozoo.Framework.Managers;
using XLua;

[RequireComponent(typeof(TMP_Text))]
public class LangTextMeshPro : MonoBehaviour
{
    
    /// <summary>
    /// Identifier
    /// </summary>
    public string       m_langId = "";

    private TMP_Text m_text;
    private LuaTable    m_langTable;
#if UNITY_EDITOR
    void OnValidate()
    {
        if (m_text == null)
        {
            m_text = GetComponent<TMP_Text>();
        }
        if (string.IsNullOrWhiteSpace(m_text.text))
        {
            m_text.text = m_langId;
        }
    }
#endif
    void Awake()
    {
        if (m_text == null)
        {
            m_text = GetComponent<TMP_Text>();
        }

        m_langTable = LuaManager.GetTable("L_LangUtils");

        RefreshText();
    }

    public void RefreshText()
    {
        if (!string.IsNullOrEmpty(m_langId))
        {
            m_text.text = m_langTable?.Get<LuaFunction>("getLangText")?.Func<int, string, string>(0, m_langId);
        }
    }
}
